
function XX = Bcr4bp_Rot2InerMtx2D(tt , xx , pc , coord , aux)
%
% BCR4BP坐标转换，Rot -> Inertial，2D，矩阵计算
%
% 输入：
% tt：       时间 , N x 1
% xx：      旋转系状态 , N x 6
% pc_ini：惯性系中心 , 1（P1 centered）/ 2（P2 centered） , 1 x 1
% aux：    仿真参数 , 1 x 1, 结构体
% coord：坐标系, 'EMrot' / 'SB1rot'
%
% 参考：
% 2021_ZimovanSpreen, page 37-40
%
% 时间：2024年06月03日
% 作者：张晨
% 单位：CSU
% 邮箱：chenzhang@csu.ac.cn
%%%%%%%%%%%%%%%%%%%%%%%

mu = aux.EMRot.mu;
mus = aux.EMRot.mus;
as = aux.EMRot.as;
wm = aux.SB1Rot.wm;

if size(xx , 2) ~= 4
    disp(' ========== wrong! xx row num! ========== ')
    XX = [];
    return;
end

if size(tt , 2) ~= 1
    disp(' ========== wrong! tt row num! ========== ')
    XX = [];
    return;
end

sin_tt = sin(tt);
cos_tt = cos(tt);
x = xx(: , 1);
y = xx(: , 2);
vx = xx(: , 3);
vy = xx(: , 4);

if strcmp(coord , 'emrot')

    if strcmp(pc , 'p1')
        % P1 centered

        XX = [cos_tt .* (mu + x) - y .* sin_tt, ...
            sin_tt .* (mu + x) + y .* cos_tt, ...
            vx .* cos_tt - sin_tt .* (mu + x) - y .* cos_tt - vy .* sin_tt, ...
            cos_tt .* (mu + x) + vy .* cos_tt + vx .* sin_tt - y .* sin_tt];

    elseif strcmp(pc , 'p2')
        % P2 centered

        XX = [cos_tt .* (mu + x - 1) - y .* sin_tt, ...
            sin_tt .* (mu + x - 1) + y .* cos_tt, ...
            vx .* cos_tt - sin_tt .* (mu + x - 1) - y .* cos_tt - vy .* sin_tt, ...
            cos_tt .* (mu + x - 1) + vy .* cos_tt + vx .* sin_tt - y .* sin_tt];

    else

        fprintf('wrong!')
        XX = [];
        return;

    end

elseif strcmp(coord , 'sb1rot')

    if strcmp(pc , 'p1')
        % P1 centered

        XX = [cos_tt .* (x - mus / (mus + 1) + (mu * cos(tt * wm)) / as) - sin_tt .* (y + (mu * sin(tt * wm)) / as), ...
            sin_tt .* (x - mus / (mus + 1) + (mu * cos(tt * wm)) / as) + cos_tt .* (y + (mu * sin(tt * wm)) / as), ...
            cos_tt .* (vx - (mu * wm * sin(tt * wm)) / as) - cos_tt .* (y + (mu * sin(tt * wm)) / as) - sin_tt .* (x - mus / (mus + 1) + (mu * cos(tt * wm)) / as) - sin_tt .* (vy + (mu * wm * cos(tt * wm)) / as), ...
            cos_tt .* (x - mus / (mus + 1) + (mu * cos(tt * wm)) / as) - sin_tt .* (y + (mu * sin(tt * wm)) / as) + cos_tt .* (vy + (mu * wm * cos(tt * wm)) / as) + sin_tt .* (vx - (mu * wm * sin(tt * wm)) / as)];

    elseif strcmp(pc , 'p2')
        % P2 centered

        XX = [cos_tt .* (x - mus / (mus + 1) + (cos(tt * wm) * (mu - 1)) / as) - sin_tt .* (y + (sin(tt * wm) * (mu - 1)) / as), ...
            sin_tt .* (x - mus / (mus + 1) + (cos(tt * wm) * (mu - 1)) / as) + cos_tt .* (y + (sin(tt * wm) * (mu - 1)) / as), ...
            cos_tt .* (vx - (wm * sin(tt * wm) * (mu - 1)) / as) - sin_tt .* (vy + (wm * cos(tt * wm) * (mu - 1)) / as) - sin_tt .* (x - mus / (mus + 1) + (cos(tt * wm) * (mu - 1)) / as) - cos_tt .* (y + (sin(tt * wm) * (mu - 1)) / as), ...
            cos_tt .* (vy + (wm * cos(tt * wm) * (mu - 1)) / as) + sin_tt .* (vx - (wm * sin(tt * wm) * (mu - 1)) / as) + cos_tt .* (x - mus / (mus + 1) + (cos(tt * wm) * (mu - 1)) / as) - sin_tt .* (y + (sin(tt * wm) * (mu - 1)) / as)];

    else

        fprintf('wrong!')
        XX = [];
        return;

    end

end

end
